use strict;
use warnings;
use Getopt::Long;
use File::Find;
use LWP::Simple;
use HTML::LinkExtor;
use URI::URL;
use DBI;

use vars qw($path $file $help $filename @files @filename $filespath);

print "---------------------Parsing Test Data-----------------------\n";
chomp($path="C:\\store_logs");
opendir(DIR, "$path") || die $!;
@filename = readdir(DIR);
close DIR;

@files = sort{ -M "$path/$b" <=> -M "$path//$a" } @filename;
my $dbh = DBI->connect("DBI:mysql:database=Ovistore;host=127.0.0.1","root","", {'RaiseError' => 1});

my $count = -1;
while(($files[$count] eq '.')&& ($files[$count] eq '..') ){
 $count--;
}

my ($lastfoler)=@_;

$lastfoler= $files[$count];
##my $sql = "SELECT log_time FROM `e2edata` where id IN(SELECT MAX(id) FROM `e2edata`);";
##my $last_update_time = $dbh->prepare($sql);


##$last_update_time->execute() or die "MySQL Error:$dbh->errstr"; 
##print "Last update time is:" +$last_update_time;

my @testresultitems = &parsedata($lastfoler);
##Verify duplicate.
my $rows = $dbh->do("INSERT INTO e2edata (service_id, model, system, log_date, log_time, result, homepage_time, homepage_result, signin_time, sign_in_result, download_speed, download_result) VALUES ('$testresultitems[0]', '$testresultitems[1]', '$testresultitems[2]', '$testresultitems[3]', '$testresultitems[4]', '$testresultitems[5]', '$testresultitems[7]', '$testresultitems[6]', '$testresultitems[9]', '$testresultitems[8]', '$testresultitems[11]', '$testresultitems[10]')");
$dbh->disconnect();



sub parsedata{
  my ($foler_name)=@_;
  my ($temp,$Searchkey)=@_; 
  $temp = "$path\\$foler_name";
  $Searchkey = "LaunchTime";
  my @LaunchTimeresult = &search($temp,$Searchkey);
  #print "LaunchTime is:\n@LaunchTimeresult\n";

  $Searchkey ="SignInTime";
  my @SignInTimeresult = &search($temp,$Searchkey);
  #print "SignInTime is:\n@SignInTimeresult\n";

  $Searchkey ="VideoDownloading";
  my @VideoDownloadingresult = &search($temp,$Searchkey);
  #print "VideoDownload is:\n@VideoDownloadingresult\n\n\n";
  
  my @Timetemp = &gettime($temp);
  
  my $service_id = 1;
  my $model = "5800";
  my $system = "S60v5";
  my $log_date= $Timetemp[0];
  my $log_time= $Timetemp[1];

  my $totalresult = $LaunchTimeresult[0] && $SignInTimeresult[0] && $VideoDownloadingresult[0];
  #print "The total result is :$totalresult\n";
  
  my @ResultItems =($service_id,$model,$system,$log_date,$log_time,$totalresult,@LaunchTimeresult,@SignInTimeresult,@VideoDownloadingresult);
  print "The Result Item is: \n@ResultItems\n\n\n";
  return @ResultItems;
}

sub gettime{
  my ($timeFile)=@_;
  my $filepath = "$timeFile\\testrun.log";
  chomp($filepath);
  my ($line);
  open (FIE, $filepath) || die("open $filepath fail\n");
  $line=<FIE>;
  chomp($line);
  my @lineitem = split(" ",$line);
  #print "Date is: $lineitem[0] and Time is $lineitem[1]\n";
  close FIE;
  my @returntime= ($lineitem[0],$lineitem[1]);
  return @returntime;
}

sub search {
  #print "Start Search\n";
	# Search a text file for a string
	my ($File,$key)=@_;
	#print "$File\n";
  my $filepath = "$File\\testrun.log";
  chomp($filepath);
  #print "Full file path is:\n $filepath\n";
  #print "Search Key is: $key\n";
	my ($line);
	my @result = (0, 0.000);
	my  $fail="";
	my  $sum;
	open (FIE, $filepath) || die("open $filepath fail\n");
	#print "Open file OK\n";
  while ($line=<FIE>)	{
	    chomp($line);
		if ($line =~ /kw_StartTimer timerID=$key/)
		{
		$result[0] = 1;
		##print "find:-$key----$result[0]\n";
		}

	    if ($line =~ /$key:/)	{
	     #print "tag\n";
		   #my $tmp=$fail;
		   #$tmp =~ s/\s+\:\s+INFO.*//g;
		   #$tmp =~ s/\..*\s*\+.*//g;
		   $line =~ s/.*$key\:\s+//g;
		   $line =~ s/\s+ms\.//g;
		   $line=$line/1000;
		   ##print "The Time is: $line\n";
		   #print "$tmp";
		   $result[1] = $line;
		   ##print "$result[1]\n";
       #$sum=$sum+$line;

		}
	}

  close FIE;
  if ($result[1] eq 0){
  $result[0] = 0;
  }
  #print "End Search\n";
  return @result;
} # End sub search

sub Usage {
print <<EndUsage;

Usage: search [-r] -d dir keywords
Options:
    -d    Directory to search test data
EndUsage
exit(0);
}

